package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.OrderprofitdetailService;
import com.youi.entity.Orderprofitdetail;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 订单分佣信息控制层
* @author Administrator
*/
@Controller
public class OrderprofitdetailController  extends HttpRequestManage{

		@Autowired
		private OrderprofitdetailService orderprofitdetailService;

		/**
		 *添加或编辑订单分佣信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditOrderprofitdetail")
		public String addOrEditOrderprofitdetail(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Orderprofitdetail orderprofitdetail=null;
			if (id!=null) {
				orderprofitdetail=orderprofitdetailService.get(id);
			}else {
				orderprofitdetail=new Orderprofitdetail();
				orderprofitdetail.setDelStatus(DelStatus.NODEL);
				orderprofitdetail.setGmtCreated(new Date());
			}
			Integer type=optInteger("type",request);
			if (type!=null){
				orderprofitdetail.setType(type);
			}
			String orderNo=optString("orderNo",request);
			if (orderNo!=null){
				orderprofitdetail.setOrderNo(orderNo);
			}
			Double orderAmount=optDouble("orderAmount",request);
			if (orderAmount!=null){
				orderprofitdetail.setOrderAmount(orderAmount);
			}
			Double commission=optDouble("commission",request);
			if (commission!=null){
				orderprofitdetail.setCommission(commission);
			}
			Long leaderId1=optLong("leaderId1",request);
			if (leaderId1!=null){
				orderprofitdetail.setLeaderId1(leaderId1);
			}
			String firstUser=optString("firstUser",request);
			if (firstUser!=null){
				orderprofitdetail.setFirstUser(firstUser);
			}
			Double firstAmount=optDouble("firstAmount",request);
			if (firstAmount!=null){
				orderprofitdetail.setFirstAmount(firstAmount);
			}
			Long leaderId2=optLong("leaderId2",request);
			if (leaderId2!=null){
				orderprofitdetail.setLeaderId2(leaderId2);
			}
			String secondUser=optString("secondUser",request);
			if (secondUser!=null){
				orderprofitdetail.setSecondUser(secondUser);
			}
			Double secondAmount=optDouble("secondAmount",request);
			if (secondAmount!=null){
				orderprofitdetail.setSecondAmount(secondAmount);
			}
			Double platAmount=optDouble("platAmount",request);
			if (platAmount!=null){
				orderprofitdetail.setPlatAmount(platAmount);
			}
			Long merchantId=optLong("merchantId",request);
			if (merchantId!=null){
				orderprofitdetail.setMerchantId(merchantId);
			}
			Double merAmount=optDouble("merAmount",request);
			if (merAmount!=null){
				orderprofitdetail.setMerAmount(merAmount);
			}
			orderprofitdetail.setGmtModify(new Date());
			orderprofitdetailService.saveOrUpdate(orderprofitdetail);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除订单分佣信息
		*/
		@ResponseBody
		@RequestMapping("delOrderprofitdetailByIds")
		public String delOrderprofitdetailByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Orderprofitdetail> orderprofitdetailList= orderprofitdetailService.getByParams(params);
			if (orderprofitdetailList==null||orderprofitdetailList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Orderprofitdetail orderprofitdetail : orderprofitdetailList) {
				orderprofitdetail.setDelStatus(DelStatus.DEL);
			}
			orderprofitdetailService.saveOrUpdateAll(orderprofitdetailList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询订单分佣信息
		*/
		@ResponseBody
		@RequestMapping("getOrderprofitdetail")
		public String getOrderprofitdetail(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Orderprofitdetail> orderprofitdetailList=orderprofitdetailService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (orderprofitdetailList==null||orderprofitdetailList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=orderprofitdetailService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, orderprofitdetailList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询订单分佣信息
		*/
		@ResponseBody
		@RequestMapping("getOrderprofitdetailById")
		public String getOrderprofitdetailById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Orderprofitdetail orderprofitdetail=orderprofitdetailService.get(id);
			if (orderprofitdetail==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",orderprofitdetail);//jsonObject	json对象
		}

		/**
		 * 查询所有订单分佣信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllOrderprofitdetail")
		public String getAllOrderprofitdetail(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Orderprofitdetail> orderprofitdetailList= orderprofitdetailService.getByParams(null);
			if (orderprofitdetailList==null||orderprofitdetailList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",orderprofitdetailList);//jsonObjectList	json对象集合
		}
}
